package com.bj.disc.ui.login.fragment

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.ai3d.rfid.R
import com.ai3d.rfid.base.fragment.BaseVmVdbFragment
import com.ai3d.rfid.common.log.Logcat
import com.ai3d.rfid.common.proxy.ToastProxy
import com.ai3d.rfid.common.utils.HandlerUtils
import com.ai3d.rfid.databinding.FragmentRegisterBinding
import com.bj.disc.ui.login.bean.RegisterReq
import com.bj.disc.util.DiscConstants.REGISTER_GENDER_MAN
import com.bj.disc.util.DiscConstants.REGISTER_GENDER_WOMAN
import com.bj.disc.ui.login.utils.FocusUtils
import com.bj.disc.ui.login.utils.RegexUtils
import com.bj.disc.ui.login.vm.RegisterViewModel
import com.bj.disc.util.StringUtil


class RegisterFragment :
    BaseVmVdbFragment<RegisterViewModel, FragmentRegisterBinding>() {
    private val TAG = "RegisterFragment"
    private val action: Action by lazy { Action() }
    private val yearList = mutableListOf<String>()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        mDatabind.action = action
        HandlerUtils.postDelayed({ hideSoftKeyboard(requireActivity()) }, 200)
    }

    override fun initData() {
        super.initData()
        for (index in 1900 until 2023) {//123
            yearList.add(index.toString())
        }
        mDatabind.wheelPicker.data = yearList
        mDatabind.wheelPicker.selectedItemPosition = 70
        mViewModel.birthday.value = (1900 + 70)
        mDatabind.wheelPicker.setOnItemSelectedListener { p0, p1, p2 ->
            mViewModel.birthday.value = (p2 + 1900)
        }

        mDatabind.tvRace.setText(R.string.malay)
    }

    override fun createObserver() {

        mViewModel.verSmsResult.observe(this) { resultState ->
            mDatabind.tbtnSms.startTimer()
            if (resultState?.verfyCode?.isNotEmpty() == true) {
                mViewModel.verifyCode.value = resultState.verfyCode
                mDatabind.executePendingBindings()
            }
        }

        mViewModel.registerResult.observe(this) { resultState ->
//            showMessage(R.string.register_successfully, positiveAction = {
            ToastProxy.showLong(StringUtil.loadString(R.string.register_successfully))
            activity?.finish()

//            })
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        HandlerUtils.removeCallbacksAndMessages()
    }

    inner class Action {

        fun back() {
            activity?.finish()
        }

        fun getRegisterSms() {
            if (mDatabind.tbtnSms.isEnabled) {
                when {
                    mViewModel.account.value?.isEmpty() == true -> {
                        ToastProxy.showLong(R.string.please_enter_your_phone_or_email)
//                        showMessage(R.string.please_enter_your_phone_or_email)
                    }
//                    !RegexUtils.isEmail(mViewModel.account.value) && !RegexUtils.isPhone(mViewModel.account.value) -> {
//                        ToastProxy.showLong(R.string.phone_or_email_incorrect_format)
////                        showMessage(R.string.phone_or_email_incorrect_format)
//                    }
                    else -> {
                        mDatabind.tbtnSms.isEnabled=false
                        mViewModel.sendVerifyCode(
                            mViewModel.account.value!!
                        )
                    }

                }
            }
        }

        fun register() {
            Logcat.d(
                TAG,
                "register account : ${mViewModel.account.value}  是否是邮箱 ${
                    RegexUtils.isEmail(mViewModel.account.value)
                }"
            )
            when {
                mViewModel.account.value?.isEmpty() == true -> {
                    FocusUtils.requestFocus(mDatabind.edtAccount)
                    ToastProxy.showLong(R.string.please_enter_your_phone_or_email)
                }//showMessage(R.string.please_enter_your_phone_or_email)
//                !RegexUtils.isEmail(mViewModel.account.value) && !RegexUtils.isPhone(mViewModel.account.value) -> {
//                    FocusUtils.requestFocus(mDatabind.edtAccount)
//                    ToastProxy.showLong(R.string.phone_or_email_incorrect_format)//showMessage(R.string.phone_or_email_incorrect_format)
//                }
//                mViewModel.verifyCode.value?.isEmpty() == true -> {
//                    FocusUtils.requestFocus(mDatabind.edtVerify)
//                    ToastProxy.showLong(R.string.please_fill_in_the_verification_code)//showMessage(R.string.please_fill_in_the_verification_code)
//                }
                mViewModel.password.value?.isEmpty() == true -> {
                    FocusUtils.requestFocus(mDatabind.etPwd)
                    ToastProxy.showLong(R.string.enter_your_pin)// showMessage(R.string.enter_your_pin)
                }
                !RegexUtils.isPwdRegex(mViewModel.password.value) -> {
                    FocusUtils.requestFocus(mDatabind.etPwd)
                    ToastProxy.showLong(R.string.only_8_12_letters_number_and_underscores_can_be_entered_for_the_password)
//                    showMessage(
//                        R.string.only_8_12_letters_number_and_underscores_can_be_entered_for_the_password
//                    )
                }
                mViewModel.firstName.value?.isEmpty() == true -> {
                    FocusUtils.requestFocus(mDatabind.editFirstName)
                    ToastProxy.showLong(R.string.please_enter_nick_name)
//                    showMessage(R.string.please_enter_first_name)
                }
//                mViewModel.secondName.value?.isEmpty() == true -> {
//                    FocusUtils.requestFocus(mDatabind.editSecondName)
//                    ToastProxy.showLong(R.string.please_enter_second_name)
////                    showMessage(R.string.please_enter_second_name)
//                }
                else -> {
                    var req = RegisterReq().apply {
                        userName = mViewModel.account.value!!
                        password = mViewModel.password.value!!
                        gender = if (mViewModel.isMame.value == true) {
                            REGISTER_GENDER_MAN
                        } else {
                            REGISTER_GENDER_WOMAN
                        }
                        birthday = mViewModel.birthday.value!!
                        // (System.currentTimeMillis() - 10 * 365 * 24 * 60 * 60 * 1000).toString()//mViewModel.birthday.value!!
                        race = mViewModel.race.value!!
                        nickName = mViewModel.firstName.value!!
//                        verfyCode = mViewModel.verifyCode.value!!
//                        verfyKey = mViewModel.verSmsResult.value?.verfyKey!!
                    }
                    mViewModel.register(req)
                }
            }
        }

//        //用户协议点击
//        fun gotoUserAgreement() {
//            activity?.let { H5Activity.startH5Activity(it, H5Constants.USER_AGREEMENT_PATH) }
//        }
//
//        // 隐私协议点击
//        fun gotoPrivacyAgreement() {
//            activity?.let { H5Activity.startH5Activity(it, H5Constants.PRIVACY_POLICY_PATH) }
//        }

        fun toggleShowPwd() {
            mViewModel.isShowPwd.value = (!mViewModel.isShowPwd.value!!)
            mDatabind.etPwd.transformationMethod = when (mViewModel.isShowPwd.value) {
                true -> HideReturnsTransformationMethod.getInstance()
                else -> PasswordTransformationMethod.getInstance()
            }
            mDatabind.etPwd.setSelection(mDatabind.etPwd.text.length)
        }

        fun clickRace() {
//            RacePop(requireActivity(), object : RacePop.RaceLister {
//                override fun onRace(raceStr: String, race: Int) {
//                    mDatabind.tvRace.text = raceStr
//                    mViewModel.race.value = race
//                }
//            }).show(mDatabind.llRace)
        }
    }

}